package edu.java.ishop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.management.RuntimeErrorException;
import javax.sql.DataSource;

import edu.java.ishop.dao.ProductDao;
import edu.java.ishop.dao.accessors.DatabaseDatasourceAccessor;
import edu.java.ishop.model.Product;
import edu.java.ishop.model.ProductCategory;
import edu.java.ishop.utils.ApplicationConfiguration;
@Named("databaseProductDao")
public  class ProductDatabasaDao extends DatabaseDatasourceAccessor implements
		ProductDao {
	
	@Inject
	private DataSource dataSource;
	//
	public ProductDatabasaDao(){
	}
	@Override
	public boolean save(Product model) {
		try(//Connection conn = openAccess();
				Connection conn = dataSource.getConnection();
				PreparedStatement pstmt = conn.prepareStatement("insert into product(name,price, discription,categoryid) values (?, ?, ?, ?)");
				
				){
			pstmt.setString(1, model.getName());
			pstmt.setBigDecimal(2, model.getPrice());
			pstmt.setString(3, model.getDescription());
			pstmt.setLong(4, model.getCategory().getId());
			return pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block  
			throw new RuntimeException(e);
		}

	}

	@Override
	public void delete(long Id) {
		try(//Connection conn = openAccess();
				Connection conn = dataSource.getConnection();
				PreparedStatement pstmt = conn.prepareStatement("delete from product where id = ?");
				){
			pstmt.setLong(1, Id);
			//pstmt.execute();
			if (!pstmt.execute()) {
				// TODO:log
			}
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public Product getById(long Id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Product> getAll() {
		List<Product> result = new ArrayList<>();
		
		try(//Connection conn = openAccess();
				Connection conn = dataSource.getConnection();
				PreparedStatement pstmt = conn.prepareStatement("select * from product");
				ResultSet rs =pstmt.executeQuery();
				
				//
				PreparedStatement pstmt2 = conn.prepareStatement("select * from productcategory where id = ?");
				//
				){
			
			while(rs.next()){
				
				Product p = new Product(rs.getLong(1));
				p.setName(rs.getString(2));
				p.setPrice(rs.getBigDecimal(3));
				p.setDescription(rs.getString(4));
				ProductCategory pc = new ProductCategory(rs.getLong(5),"");
				
				//
				pstmt2.setLong(1, pc.getId());
				ResultSet rs2 =pstmt2.executeQuery();
				if(rs2.next()){
					pc.setName(rs2.getString(2));
				}
				//
				
				p.setCategory(pc);
				result.add(p);
			}
			
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new RuntimeException();
		}
		return result;
	}

	@Override
	public List<Product> getByCategoty(ProductCategory category) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void delete(Product product) {
		delete(product.getId());
		
	}
	@Override
	public void update(Product model) {
		try(//Connection conn = openAccess();
				Connection conn = dataSource.getConnection();
				PreparedStatement pstmt = conn.prepareStatement("update product set name=?, price=?, discription=?, categoryid=? where id=?");
				
				){
			pstmt.setString(1, model.getName());
			pstmt.setBigDecimal(2, model.getPrice());
			pstmt.setString(3, model.getDescription());
			pstmt.setLong(4, model.getCategory().getId());
			pstmt.setLong(5, model.getId());
			pstmt.execute();
			//return pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block  
			throw new RuntimeException(e);
		}

	}
	
	
//	@Override
//	protected String getPassword() {
//		return ApplicationConfiguration.getItem("db.password");
//	}
//
//	@Override
//	protected String getUsername() {
//		return ApplicationConfiguration.getItem("db.username");
//	}
//
//	@Override
//	protected String getConnectionUrl() {
//		return ApplicationConfiguration.getItem("db.connectionUrl");
//	}

}
